package com.shxc.smarttrade.quotation.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.quotation.model.BriefModel;

import java.util.List;

/**
 * 行情 - 自选 - 上方
 */

public class QuotatonChooseTopAdapter extends RecyclerView.Adapter<QuotatonChooseTopAdapter.ViewHolder> {

    private Context mContext;
    private List<BriefModel> mList;
    private Handler mHandler;

    public QuotatonChooseTopAdapter(Context c, List<BriefModel> list, Handler handler) {
        mContext = c;
        mList = list;
        mHandler = handler;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_quotation_choose_top, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final BriefModel model = mList.get(position);
        model.setPosition(position);

//        holder.symbolcn_textview.setText(model.getSymbolCn());
//        holder.symbolen_textview.setText(model.getSymbolEn());
        holder.symbolcn_textview.setText(model.getSymbolEn());
        holder.symbolen_textview.setText(model.getSymbolCn());

        if (model.getSymbolEn().equals("")) {
            holder.item_relativelayout.setBackgroundResource(R.drawable.box_round_quotation_choose);
        } else {
            holder.item_relativelayout.setBackgroundResource(R.drawable.bg_round_quotation_choose);
            //跳转页面
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = model;
                    mHandler.sendMessage(msg);

                    mList.remove(model);
                    notifyItemRemoved(position);
                    if (position != mList.size()) { // 如果移除的是最后一个，忽略
                        notifyItemRangeChanged(position, mList.size() - position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void addModel(BriefModel bm) {

        if (mList.size() == 15) {
            mHandler.sendEmptyMessage(3);
        } else {
            if (mList.size() == 1 && mList.get(0).getSymbolEn().equals("")) {
                mList.clear();
            }
            mList.add(bm);
            notifyDataSetChanged();
            mHandler.sendEmptyMessage(4);
        }
    }

    public List<BriefModel> getList() {
        return mList;
    }

    public void setNullList(List<BriefModel> nullList) {
        mList = nullList;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        protected RelativeLayout item_relativelayout;//中文
        protected TextView symbolcn_textview;//中文
        protected TextView symbolen_textview;//英文

        public ViewHolder(View itemView) {
            super(itemView);
            item_relativelayout = (RelativeLayout) itemView.findViewById(R.id.item_relativelayout);
            symbolcn_textview = (TextView) itemView.findViewById(R.id.symbolcn_textview);
            symbolen_textview = (TextView) itemView.findViewById(R.id.symbolen_textview);
        }
    }

}
